/**
 * 
 */
package com.worksmap.core.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.worksmap.core.exception.WebUtilException;

/**
 * @author jack
 *
 */
@SuppressWarnings("deprecation")
public class WebUtil {
	
	public static String postMethod(String url, List<NameValuePair> params) throws WebUtilException{
		String body = null;
        try {
        	HttpClient httpclient = new DefaultHttpClient();
            // Post请求
            HttpPost httppost = new HttpPost(url);
            // 设置参数
            httppost.setEntity(new UrlEncodedFormEntity(params));
            // 发送请求
            HttpResponse httpresponse = httpclient.execute(httppost);
            // 获取返回数据
            HttpEntity entity = httpresponse.getEntity();
            body = EntityUtils.toString(entity);
            if (entity != null) {
                entity.consumeContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new WebUtilException(e.getMessage(), e.getCause());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw new WebUtilException(e.getMessage(), e.getCause());
        } catch (ParseException e) {
            e.printStackTrace();
            throw new WebUtilException(e.getMessage(), e.getCause());
        } catch (IOException e) {
            e.printStackTrace();
            throw new WebUtilException(e.getMessage(), e.getCause());
        } catch(IllegalStateException e){
        	e.printStackTrace();
        	throw new WebUtilException(e.getMessage(), e.getCause());
        }catch(Exception e){
        	e.printStackTrace();
        	throw new WebUtilException(e.getMessage(), e.getCause());
        }
        return body;
	}

}
